package com.base.api.service.commodityReferrer.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.api.dto.commodityReferrer.ReferrerListDTO;
import com.base.api.service.commodityReferrer.CommodityReferrerApiService;
import com.base.api.vo.commodityReferrer.ReferrerVO;
import com.base.result.PageUtils;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.entity.Commodity;
import com.base.self.entity.CommodityReferrer;
import com.base.self.mapper.CommodityMapper;
import com.base.self.mapper.CommodityReferrerMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class CommodityReferrerApiServiceImpl implements CommodityReferrerApiService {

    private final CommodityReferrerMapper commodityReferrerMapper;

    private final CommodityMapper commodityMapper;

    public CommodityReferrerApiServiceImpl(CommodityReferrerMapper commodityReferrerMapper, CommodityMapper commodityMapper) {
        this.commodityReferrerMapper = commodityReferrerMapper;
        this.commodityMapper = commodityMapper;
    }

    @Override
    @Transactional(readOnly = true)
    public ResponseResult referrerList(ReferrerListDTO referrerListDTO) {
        final Map<String, Object> data = PageUtils.getDateMapBack(() ->
                        commodityReferrerMapper.selectPage(
                                PageUtils.getPage(
                                        new Page<>(),
                                        referrerListDTO.getOffset(),
                                        referrerListDTO.getLimit()),
                                new QueryWrapper<CommodityReferrer>()
                                        .eq("is_del", 0)
                                        .orderByDesc("create_time")
                        )
                        ,
                        page -> {
                            List<CommodityReferrer> data1 = page.getRecords();
                            return data1.stream().map(x -> {
                                final Commodity commodity = commodityMapper.selectById(x.getCommodityId());
                                return new ReferrerVO(
                                        commodity.getId(),
                                        commodity.getCommodityName(),
                                        commodity.getCommodityRealPrice(),
                                        commodity.getCommodityMarketPrice(),
                                        commodity.getCommoditySales(),
                                        commodity.getCommodityShowPic()
                                );
                            });
                        });
        return ResponseResultUtils.getResponseResultS("查询成功", data);
    }
}
